package com.arvin.faceDetect.ui.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.ListAdapter
import androidx.recyclerview.widget.RecyclerView
import com.arvin.faceDetect.databinding.ItemRecognitionRecordBinding
import com.arvin.faceDetect.db.RecognitionRecord
import java.text.SimpleDateFormat
import java.util.Locale

class RecognitionRecordAdapter :
    ListAdapter<RecognitionRecord, RecognitionRecordAdapter.ViewHolder>(
        object : DiffUtil.ItemCallback<RecognitionRecord>() {
            override fun areItemsTheSame(
                oldItem: RecognitionRecord,
                newItem: RecognitionRecord
            ): Boolean {
                return oldItem.id == newItem.id
            }

            override fun areContentsTheSame(
                oldItem: RecognitionRecord,
                newItem: RecognitionRecord
            ): Boolean {
                return oldItem == newItem
            }
        }
    ) {
    class ViewHolder(private val binding: ItemRecognitionRecordBinding) :
        RecyclerView.ViewHolder(binding.root) {

        fun bind(record: RecognitionRecord) {
            binding.tvName.text = record.name
            binding.tvTime.text = SimpleDateFormat(
                "yyyy-MM-dd HH:mm:ss",
                Locale.getDefault()
            ).format(record.getDate())
            binding.tvSimilarity.text = String.format("相似度：%.2f%%", record.similarity * 100)

            // 显示人脸图像
            record.faceBitmap?.let { bitmap ->
                binding.ivFace.setImageBitmap(bitmap)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(
            ItemRecognitionRecordBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(getItem(position))
    }
} 